﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Xml;

namespace GCard
{
    public partial class FileForm : Form
    {
        public FileForm()
        {
            InitializeComponent();
        }

        private void btn_File_Click(object sender, EventArgs e)
        {
            if (!File.Exists("1.txt"))
            {
                File.Create("1.txt").Close();
            }
            else
            {
                File.Delete("1.txt");
                File.Create("1.txt").Close();
            }
            Console.WriteLine("操作成功！")
            ;
        }

        private void btn_copy_Click(object sender, EventArgs e)
        {
            if (!File.Exists(@"C:\Users\Admin\Desktop\new.txt"))
            {
                File.Copy("1.txt", @"C:\Users\Admin\Desktop\new.txt");
            }
            else
            {
                File.Delete(@"C:\Users\Admin\Desktop\new.txt");
                File.Create(@"C:\Users\Admin\Desktop\new1.txt").Close();
            }
            Console.WriteLine("复制成功");

        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            File.Delete(@"C:\Users\Admin\Desktop\new.txt");
            File.Delete(@"C:\Users\Admin\Desktop\new1.txt");
            File.Delete("1.txt");
            File.Delete("2.txt");
        }

        private void btn_readStr_Click(object sender, EventArgs e)
        {
            //读取文本文件字符串；
            string str = File.ReadAllText(@"C:\Users\Admin\Desktop\异常与处理.txt",Encoding.Default);
            Console.WriteLine(str);
            File.WriteAllText(@"C:\Users\Admin\Desktop\1.txt", str, Encoding.Default);
        }

        private void btn_readbyte_Click(object sender, EventArgs e)
        {
            //读取所有文件字节
            byte[] buffer = File.ReadAllBytes(@"C:\Users\Admin\Desktop\异常与处理.txt");
            //将字节转换为字符串,解码
            string str = Encoding.Default.GetString(buffer);
 
            Console.WriteLine(str);
        }
       
        //
        private void btn_filestreamRead_Click(object sender, EventArgs e)
        {
            //1、创建对象
            using (FileStream fs = new FileStream(@"C:\Users\Admin\Desktop\每日进度\GCard\GCard\text.txt", FileMode.Open, FileAccess.Read))//@"C:\Users\Admin\Desktop\每日进度\GCard\GCard\text.txt"
            {
                //2、创建缓存区大小     例子：迅雷下载：先在内存区中开辟空间累计一定数据量后一起写入硬盘，减少对硬盘的操作次数
                byte[] buffer = new byte[1024 * 1024 * 5];
                //3、读取数据放入缓存区
                int r = fs.Read(buffer, 0, buffer.Length);
                //4、将字节转换成字符串
                string content = Encoding.Default.GetString(buffer, 0, r);
                //手动释放资源
                //fs.Close();
                //fs.Dispose();
                Console.WriteLine(content);
            }
        }

        private void btn_streamreader_Click(object sender, EventArgs e)
        {
            using (StreamReader streamReader = new StreamReader(@"C:\Users\Admin\Desktop\每日进度\GCard\GCard\text.txt"))
            {
                while (!streamReader.EndOfStream)//没读完就一直读
                {
                    Console.WriteLine(streamReader.ReadLine());
                }
            }
        }

        private void btn_streamwriter_Click(object sender, EventArgs e)
        {
            using (StreamWriter streamWriter = new StreamWriter(@"text1.txt",false))//第二个参数为是否追加，或覆盖
            {
                streamWriter.WriteLine("12345679aaaa啊啊啊");
                Console.WriteLine();

            }
        }

        private void btn_directory_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(@"C:\Users\Admin\Desktop\每日进度\GCard\GCard\123"))
            {
                Directory.CreateDirectory(@"C:\Users\Admin\Desktop\每日进度\GCard\GCard\123");
                Console.WriteLine("创建文件夹成功！");
            }
            else
            {
                Directory.Delete(@"C:\Users\Admin\Desktop\每日进度\GCard\GCard\123",false);//若要移除 path 中的目录、子目录和文件，则为 true；否则为 false。
                Directory.CreateDirectory(@"C:\Users\Admin\Desktop\每日进度\GCard\GCard\123");
                Console.WriteLine("先删除后创建文件夹成功！");
            }
        }

        private void btn_movedirectory_Click(object sender, EventArgs e)
        {
            //只能在同一盘中操作
            //Directory.Move(@"C:\Users\Admin\Desktop\每日进度\GCard\GCard\123", @"C:\Users\Admin\Desktop");
            //获得指定文件夹下所有文件路径的全路径
            string[] aa = Directory.GetFiles(@"C:\Users\Admin\Desktop\每日进度", "*.txt"); //要与 path 中的文件名匹配的搜索字符串。
            foreach (string a in aa)
            {
                Console.WriteLine(a);
            }

            string[] ab = Directory.GetDirectories(@"C:\Users\Admin\Desktop\每日进度"); //获得指路径下所有文件夹
            foreach (string a in ab)
            {
                Console.WriteLine(a);
            }

        }

        private void btn_md5_Click(object sender, EventArgs e)
        {
            //3244185981728979115075721453575112
            //202cb962ac5975b964b7152d234b70
            //202cb962ac59075b964b07152d234b70
            string a = XY.GetMD5("123");
            Console.WriteLine(a);
        }

        private void btn_xml_Click(object sender, EventArgs e)
        {
            //1、创建xml文档对象
            XmlDocument doc = new XmlDocument();
            //2、创建一个行描述信息
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //3、创建根节点
            XmlElement order = doc.CreateElement("Order");
            doc.AppendChild(order);
            //4、给根节点添加字节点
            XmlElement customName = doc.CreateElement("customName");
            customName.InnerXml = "liuyang";
            order.AppendChild(customName);

            XmlElement orderNumber = doc.CreateElement("OrderNumber");
            orderNumber.InnerXml = "bj";
            order.AppendChild(orderNumber);



            doc.Save("order.xml");
        }



    }
}
